<template>
	<view class="container">
		<view v-if="Object.keys(resInfo).length !== 0">
			<view class="f-top">
				<view class="f-top-top" style="color: #000000;">找零工平台支付</view>
				<view class="f-top-meny">
					<text style="font-size: 48rpx;margin-top: -10rpx;color: #000000;">￥</text>
					<text style="color: #000000;">{{jine}}</text>
				</view>
			</view>
			<view class="f-mid">
				<text style="color: #999;padding-left: 50rpx;">收款方</text>
				<text style="padding-right: 50rpx;color: #000000;">ZG-找零工</text>
			</view>
			<view class="f-bottom">
				<view class="f-bottom-center" @click="mpsPay"> 立即支付 </view>
			</view>
		</view>
		<u-modal :show="isShow" closeOnClickOverlay :showConfirmButton="false" width="520rpx">
			<view class="lert-main">
				<view class="tip">
					<image src="https://images.linglinggong.net/static/cg.png" mode="widthFix" border="0"></image>
					<text decode="true" class="reg_ok_20">支付成功</text>
					<!-- <text decode="true" class="reg_ok_21" style="min-height: 110rpx;">为了保障信息安全,请务必保管好您的身份信息以及账号密码</text> -->
				</view>
				<view class="bt-tn">
					<button open-type="launchApp" :app-parameter="resInfo.trade" binderror="launchAppError"
						@click="handleConfirm" class="btOk">返回APP</button>
				</view>
			</view>
		</u-modal>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				jine: 0,
				type: '',
				resUrlType: '',
				resInfo: {},
				orderInfo: {
					oppenid: '',
					payType: 4
				},
				isShow: false,
				isBuyYk: 'false'
			}
		},
		onLoad(option) {
			this.type = option.type
			if (this.type == 'fabu') {
				this.isBuyYk = option.isBuyYk
				if (this.isBuyYk == 'true') {
					this.orderInfo.cardId = option.cardId
				} else {
					this.orderInfo.taskId = option.taskId
				}
			}
			if (this.type == 'card') {
				this.resUrlType = option.resUrlType
				if (this.resUrlType == 0) {
					this.orderInfo.cardId = option.cardId
				} else {
					this.orderInfo.cardId = 0
					this.orderInfo.carddosageId = option.carddosageId
				}
			}
			if (this.type == 'jiefeng') {
				this.orderInfo.unsealId = option.unsealId
			}
			if (this.type == 'chongzhi') {
				this.orderInfo.amount = option.amount
			}
			if (this.type == 'jiesuan1') {
				let tempForm = JSON.parse(option.taskInfo)
				this.orderInfo.amount = tempForm.amount
				this.orderInfo.couponId = tempForm.couponId
				this.orderInfo.orderId = tempForm.orderId
				this.orderInfo.taskId = tempForm.taskId
			}
			if (this.type == 'jiesuan2') {
				let tempForm = JSON.parse(option.taskInfo)
				this.orderInfo.couponId = tempForm.couponId
				this.orderInfo.orderParam = tempForm.orderParam
				this.orderInfo.taskId = tempForm.taskId
			}
			if (this.type == 'goldMedal') {
				delete this.orderInfo.taskId
				delete this.orderInfo.cardId
				delete this.orderInfo.unsealId
				delete this.orderInfo.amount
				delete this.orderInfo.couponId
				delete this.orderInfo.orderId
				delete this.orderInfo.orderParam
				delete this.orderInfo.carddosageId
			}
			uni.setStorageSync('token', option.token)
			this.getUserT()
		},
		methods: {
			handleConfirm() {
				this.isShow = false
			},
			getUserT() {
				uni.showLoading({
					title: '请稍后...'
				})
				uni.login({
					provider: 'weixin',
					success: res => {
						let code = res.code
						uni.getUserInfo({
							provider: 'weixin',
							success: infoRes => {
								// uni.setStorageSync('userInfo', infoRes.userInfo)
								// console.log(2,code);
								this.getWxCode(code)
							}
						});
					},
					fail: (err) => {
						uni.showToast({
							title: '授权失败',
							icon: 'none'
						})
					},
					complete() {}
				})
			},
			async getWxCode(code) {
				let res = await this.$fetch(this.$api.wx_login_xcx, {
					code: code
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				// console.log(res.data);
				this.orderInfo.oppenid = res.data.openid
				this.getData()
			},
			async getData() {
				console.log('进来类型' + this.type)
				let pay_api = ''
				if (this.type == 'fabu') {
					if (this.isBuyYk == 'true') {
						pay_api = this.$api.v1_Buy_monthly_card
					} else {
						pay_api = this.$api.v1_pay_bond
					}
				}
				if (this.type == 'card') {
					if (this.resUrlType == 0) {
						pay_api = this.$api.v1_Buy_monthly_card
					} else {
						pay_api = this.$api.v1_number_of_buyers
					}
				}
				if (this.type == 'jiefeng') {
					pay_api = this.$api.Pay_penalty
				}
				if (this.type == 'goldMedal') {
					pay_api = this.$api.pay_gold_medal
				}
				if (this.type == 'chongzhi') {
					pay_api = this.$api.v1_recharge
				}
				if (this.type == 'jiesuan1') {
					pay_api = this.$api.v1_settlement_single_task
				}
				if (this.type == 'jiesuan2') {
					pay_api = this.$api.v1_settlement_task
				}
				console.log('这是URL' + pay_api)
				console.log('这是请求参数' + JSON.stringify(this.orderInfo))
				if (!pay_api) return
				let res = ''
				if (this.type == 'jiesuan1' || this.type == 'jiesuan2') {
					res = await this.$fetch(pay_api, this.orderInfo, 'POST', 'JSON')
					console.log('这是返回参数' + JSON.stringify(res))
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					this.resInfo = JSON.parse(res.msg)
					this.jine = this.resInfo.money ? (this.resInfo.money / 100) : 0
				} else {
					res = await this.$fetch(pay_api, this.orderInfo, 'POST')
					console.log('这是请求参数209行' + JSON.stringify(this.orderInfo))
					console.log('这是返回参数210行' + JSON.stringify(res))
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					this.resInfo = res.data
					this.jine = this.resInfo.money ? (this.resInfo.money / 100).toFixed(2) : 0
					this.isBuyYk = false
				}
				this.$fetch(this.$api.save_tradeNoByuid, {
					tradeNo: this.resInfo.trade
				}, 'POST')
				uni.hideLoading()
			},
			/* 微信小程序支付 */
			mpsPay() {
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: this.resInfo.timeStamp, //时间戳
					nonceStr: this.resInfo.nonceStr,
					package: this.resInfo.package,
					signType: this.resInfo.signType,
					paySign: this.resInfo.paySign,
					success: res => {
						this.isShow = true
						console.log('success:' + JSON.stringify(res));
					},
					fail: err => {
						console.log('fail:' + JSON.stringify(err));
					}
				})
			},
		}
	}
</script>
<style lang="less" scoped>
	.container {
		.lert-main {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			background: #fff;
			border-radius: 20rpx;

			// border: 1px solid;
			.tip {
				// padding-top: 50rpx;
				// padding-bottom: 10rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;

				image {
					width: 96rpx;
					height: 100%;
				}

				.reg_ok_20 {
					padding: 25rpx 0;
					font-size: 34rpx;
					color: #000;
					// font-weight: 600;
				}

				.reg_ok_21 {
					width: 70%;
					margin: 0 auto;
					text-align: center;
					font-size: 24rpx;
					color: #c8c8c8;
				}
			}

			.bt-tn {
				padding-top: 20rpx;

				// padding-bottom: 56rpx;
				.btOk {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 330rpx;
					height: 76rpx;
					background-color: #FCC928;
					border-radius: 50rpx;
					color: #fff;
					font-size: 28rpx;
					text-align: center;
				}
			}
		}

		.f-top {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			.f-top-top {
				width: 100%;
				// border: 1px solid;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 34rpx;
				font-weight: 600;
				padding-top: 20rpx;
				padding-bottom: 10rpx;
			}

			.f-top-meny {
				width: 40%;
				margin: 0 auto;
				// padding-right: 0rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 70rpx;
				font-weight: 600;
				padding-bottom: 20rpx;
			}
		}

		.f-mid {
			width: 100%;
			height: 96rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #fff;
			// box-shadow:0px 1px 1px #E7E7E7;
			border-top: 1px solid #E7E7E7;
			border-bottom: 1px solid #E7E7E7;
		}

		.f-bottom {
			margin-top: 40rpx;
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;

			.f-bottom-center {
				width: 92%;
				height: 86rpx;
				background: #58BE6B;
				border-radius: 15rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #fff;
				font-size: 34rpx;
			}
		}
	}
</style>